import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
//引入path模块
import { resolve } from "path";
// 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
import AutoImport from "unplugin-auto-import/vite";
// 按需引入element-plus
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
export default defineConfig({
  // 设置别名
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
      "~": resolve(__dirname, "src"),
    },
    extensions: [".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
  optimizeDeps: {
    include: ["vue3-video-player"],
  },
  plugins: [
    vue(),
    AutoImport({
      imports: ["vue", "vue-router"],
    }),
    Components({
      resolvers: [ElementPlusResolver({ importStyle: "sass" })],
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [resolve("./src/assets/svg")],
      // 指定symbolId格式
      symbolId: "[name]",
    }),
  ],
  // 配置环境变量文件路径
  // envDir:"./src",
  envPrefix: ["VITE_", "APP_", "NODE_"],
  server: {
    host: "0.0.0.0",
    open: true, // 设置服务启动时是否自动打开浏览器
    cors: true, // 允许跨域
  },
  build: {
    // outDir: 'vue3-template',
    chunkSizeWarningLimit: 1600, //加大限制的大小
    emptyOutDir: true, //打包时先清空上一次构建生成的目录
    rollupOptions: {
      output: {
        // 用于命名代码拆分时创建的共享块的输出命名
        chunkFileNames: "js/[name]-[hash].js",
        // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
        entryFileNames: "js/[name]-[hash].js",
        // 用于输出静态资源的命名放到dist中的static文件夹下，[ext]表示文件扩展名
        assetFileNames: "[ext]/[name]-[hash].[ext]",
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
  },
});
